#!/bin/bash

# 用户管理API测试脚本
# 用于测试用户管理相关的API接口

# 设置颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 基础URL
BASE_URL="http://localhost:8080/retail-admin"

# 测试计数器
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0

# 测试函数
test_api() {
    local test_name="$1"
    local method="$2"
    local url="$3"
    local data="$4"
    local expected_status="$5"
    
    TOTAL_TESTS=$((TOTAL_TESTS + 1))
    
    echo -e "${BLUE}测试: $test_name${NC}"
    echo "请求: $method $url"
    
    if [ -n "$data" ]; then
        echo "数据: $data"
        response=$(curl -s -w "\n%{http_code}" -X "$method" \
            -H "Content-Type: application/json" \
            -d "$data" \
            "$url")
    else
        response=$(curl -s -w "\n%{http_code}" -X "$method" \
            -H "Content-Type: application/json" \
            "$url")
    fi
    
    # 分离响应体和状态码
    http_code=$(echo "$response" | tail -n1)
    response_body=$(echo "$response" | head -n -1)
    
    echo "状态码: $http_code"
    echo "响应: $response_body"
    
    if [ "$http_code" = "$expected_status" ]; then
        echo -e "${GREEN}✓ 通过${NC}"
        PASSED_TESTS=$((PASSED_TESTS + 1))
    else
        echo -e "${RED}✗ 失败 (HTTP $http_code)${NC}"
        FAILED_TESTS=$((FAILED_TESTS + 1))
    fi
    
    echo "----------------------------------------"
}

# 检查服务是否启动
check_service() {
    echo -e "${YELLOW}检查服务状态...${NC}"
    response=$(curl -s -o /dev/null -w "%{http_code}" "$BASE_URL/")
    if [ "$response" = "404" ] || [ "$response" = "200" ]; then
        echo -e "${GREEN}✓ 服务已启动${NC}"
    else
        echo -e "${RED}✗ 服务未启动，请先启动服务${NC}"
        exit 1
    fi
    echo "----------------------------------------"
}

# 主测试流程
main() {
    echo -e "${YELLOW}开始用户管理API测试${NC}"
    echo "========================================"
    
    # 检查服务状态
    check_service
    
    # 1. 分页查询用户列表
    test_api "分页查询用户列表" "GET" "$BASE_URL/api/v1/users?pageNum=1&pageSize=10" "" "200"
    
    # 2. 根据ID查询用户详情
    test_api "根据ID查询用户详情" "GET" "$BASE_URL/api/v1/users/1" "" "200"
    
    # 3. 创建用户
    user_data='{
        "username": "testuser",
        "password": "123456",
        "realName": "测试用户",
        "email": "test@example.com",
        "phone": "13800138000",
        "status": 1,
        "roleIds": [1]
    }'
    test_api "创建用户" "POST" "$BASE_URL/api/v1/users" "$user_data" "200"
    
    # 4. 检查用户名是否存在
    test_api "检查用户名是否存在" "GET" "$BASE_URL/api/v1/users/check-username?username=admin" "" "200"
    
    # 5. 检查邮箱是否存在
    test_api "检查邮箱是否存在" "GET" "$BASE_URL/api/v1/users/check-email?email=admin@retail.com" "" "200"
    
    # 6. 检查手机号是否存在
    test_api "检查手机号是否存在" "GET" "$BASE_URL/api/v1/users/check-phone?phone=13800138000" "" "200"
    
    # 7. 更新用户（假设用户ID为2）
    update_data='{
        "id": 2,
        "realName": "更新后的测试用户",
        "email": "updated@example.com",
        "phone": "13800138001",
        "status": 1,
        "roleIds": [1]
    }'
    test_api "更新用户" "PUT" "$BASE_URL/api/v1/users/2" "$update_data" "200"
    
    # 8. 重置用户密码
    password_data='{
        "newPassword": "newpassword123"
    }'
    test_api "重置用户密码" "POST" "$BASE_URL/api/v1/users/2/reset-password" "$password_data" "200"
    
    # 9. 删除用户（假设用户ID为2）
    test_api "删除用户" "DELETE" "$BASE_URL/api/v1/users/2" "" "200"
    
    # 测试结果汇总
    echo -e "${YELLOW}测试结果汇总${NC}"
    echo "========================================"
    echo -e "总测试数: ${BLUE}$TOTAL_TESTS${NC}"
    echo -e "通过数: ${GREEN}$PASSED_TESTS${NC}"
    echo -e "失败数: ${RED}$FAILED_TESTS${NC}"
    
    if [ $FAILED_TESTS -eq 0 ]; then
        echo -e "${GREEN}所有测试通过！${NC}"
        exit 0
    else
        echo -e "${RED}有 $FAILED_TESTS 个测试失败${NC}"
        exit 1
    fi
}

# 运行主函数
main

